﻿namespace Alkitab.Language
{
    using System;
    using System.ComponentModel;
    using System.Linq.Expressions;

    public class LabelsManager : INotifyPropertyChanged
    {
        #region Fields

        private Labels labels;

        #endregion Fields

        #region Constructors

        public LabelsManager()
        {
            labels = new Labels();
        }

        #endregion Constructors

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

        #region Properties

        public Labels Labels
        {
            get { return labels; }
        }

        #endregion Properties

        #region Methods

        public static string GetPropertyNameFromExpression<T>(Expression<Func<T>> property)
        {
            var lambda = (LambdaExpression)property;
            MemberExpression memberExpression;

            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)lambda.Body;
                memberExpression = (MemberExpression)unaryExpression.Operand;
            }
            else
            {
                memberExpression = (MemberExpression)lambda.Body;
            }

            return memberExpression.Member.Name;
        }

        public void OnPropertyChanged<T>(Expression<Func<T>> selector)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(GetPropertyNameFromExpression(selector)));
            }
        }

        public void ResetResources()
        {
            OnPropertyChanged(() => Labels);
        }

        #endregion Methods
    }
}